//Portions Copyright (C) 2011 Randall Shattuck
//Portions Copyright (C) 2007-2010 Owners of libeveapi 
//    (http://libeveapi.googlecode.com)
//
//EveDashboard.Api was forked from libeveapi 1.1.3
//
//This program is free software; you can redistribute it and/or modify it under
//the terms of the GNU General Public License as published by the Free Software 
//Foundation; either version 2 of the License, or (at your option) any later 
//version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT 
//ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
//FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
//details. You should have received a copy of the GNU General Public License 
//along with this program; if not, write to the Free Software Foundation, Inc.,
//59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//-----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;

namespace EveDashboard.Api.ResponseObjects.Parsers
{
    ///<summary>
    /// A parser which converts a given <see cref="XmlDocument"/> to a <see cref="MemberTracking"/>.
    ///</summary>
    internal class MemberTrackingResponseParser : IApiResponseParser<MemberTracking>
    {
        public MemberTracking Parse(XmlDocument xmlDocument)
        {
            this.CheckVersion(xmlDocument);
            MemberTracking memberTracking = new MemberTracking();
            memberTracking.ParseCommonElements(xmlDocument);

            List<MemberTracking.Member> parsedMemeberTrackingItems = new List<MemberTracking.Member>();
            foreach (XmlNode row in xmlDocument.SelectNodes("//rowset[@name='members']/row"))
            {
                MemberTracking.Member mti = new MemberTracking.Member();
                mti.CharacterId = Convert.ToInt32(row.Attributes["characterID"].InnerText, CultureInfo.InvariantCulture);
                mti.Name = row.Attributes["name"].InnerText;
                mti.BaseId = Convert.ToInt32(row.Attributes["baseID"].InnerText, CultureInfo.InvariantCulture);
                mti.Base = row.Attributes["base"].InnerText;
                mti.Title = row.Attributes["title"].InnerText;
                mti.LocationId = Convert.ToInt32(row.Attributes["locationID"].InnerText, CultureInfo.InvariantCulture);
                mti.Location = row.Attributes["location"].InnerText;
                mti.ShipTypeId = Convert.ToInt32(row.Attributes["shipTypeID"].InnerText, CultureInfo.InvariantCulture);
                mti.ShipType = row.Attributes["shipType"].InnerText;
                mti.RolesMask = row.Attributes["roles"].InnerText;
                mti.GrantableRoles = row.Attributes["grantableRoles"].InnerText;

                mti.StartDateTime = TimeUtilities.ConvertCCPTimeStringToDateTimeUTC(row.Attributes["startDateTime"].InnerText);
                mti.LogonDateTime = TimeUtilities.ConvertCCPTimeStringToDateTimeUTC(row.Attributes["logonDateTime"].InnerText);
                mti.LogoffDateTime = TimeUtilities.ConvertCCPTimeStringToDateTimeUTC(row.Attributes["logoffDateTime"].InnerText);
                mti.StartDateTimeLocal = TimeUtilities.ConvertCCPToLocalTime(mti.StartDateTime);
                mti.LogonDateTimeLocal = TimeUtilities.ConvertCCPToLocalTime(mti.LogonDateTime);
                mti.LogoffDateTimeLocal = TimeUtilities.ConvertCCPToLocalTime(mti.LogoffDateTime);

                parsedMemeberTrackingItems.Add(mti);
            }

            memberTracking.Members = parsedMemeberTrackingItems.ToArray();
            return memberTracking;
        }

        public void CheckVersion(XmlDocument xmlDocument)
        {
            if (MemberTracking.VersionCheck)
            {
                string version = xmlDocument.SelectSingleNode("//eveapi").Attributes["version"].InnerText;
                if (version.CompareTo(MemberTracking.API_VERSION) != 0)
                {
                    throw new ApiVersionException(version, MemberTracking.API_VERSION);
                }
            }
        }
    }
}
